package zym.diandian.tools;

import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.format.DateFormat;
import android.text.format.Formatter;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import zym.diandian.MainActivity;
import zym.diandian.R;

public class Tools {

    public static long len = 0;

    public static File copyAeest2File(Context pContext, String assetName) {
        InputStream mInputStream = null;
        FileOutputStream fos = null;

        String mPath = pContext.getFilesDir().getPath();
        File mFile = new File(mPath + "/" + assetName);

        try {
            mInputStream = pContext.getAssets().open(assetName);

            int len = 0;
            byte[] buffer = new byte[1024];
            fos = pContext.openFileOutput(assetName, Context.MODE_WORLD_READABLE);
            while ((len = mInputStream.read(buffer)) != -1) {
                fos.write(buffer, 0, len);
                fos.flush();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (Exception e) {

            }
            try {
                if (mInputStream != null) {
                    mInputStream.close();
                }
            } catch (Exception e) {

            }
        }

        Toast.makeText(pContext, "" + mFile.getPath(), Toast.LENGTH_LONG).show();
        return mFile;
    }

    //


    //
    public static String getFileSize(Context pContext, String path) {
        long lenth = 0;
        File mFile = new File(path);
        if (mFile.exists()) {
            lenth = mFile.length();
        } else {
            System.out.println("* getFileSize() " + mFile.getPath() + " 不存在");
            lenth = 0;
        }
        String str = Formatter.formatFileSize(pContext, lenth);
        return str;
    }

    public static String getIpAddress(int ipAddr) {
        StringBuilder ipBuf = new StringBuilder();
        ipBuf.append(ipAddr & 0xff).append('.').append((ipAddr >>>= 8) & 0xff).append('.').append((ipAddr >>>= 8) & 0xff).append('.').append((ipAddr >>>= 8) & 0xff);

        return ipBuf.toString();
    }

    //
    public static String inputStream2Str(InputStream pInputStream) {
        int len = 0;
        byte buffer[] = new byte[1024];
        StringBuilder mBuffer = new StringBuilder();
        if (pInputStream == null) {
            return "null";
        }
        try {
            while ((len = pInputStream.read(buffer)) != -1) {
                String str = new String(buffer, 0, len);
                System.out.println(" str :" + str);
                mBuffer.append(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return mBuffer.toString();
    }


    //
    public static boolean isRoot(Context pContext) {
        File mFile = new File("/system/bin/su");
        return mFile.exists();
    }

    /**
     * 为程序创建桌面快捷方式
     */
    public static void addShortcut(Context context) {
        Intent shortcut = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
        // 快捷方式的名称
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_NAME, context.getString(R.string.app_name));
        shortcut.putExtra("duplicate", false); // 不允许重复创建

        Intent shortcutIntent = new Intent(Intent.ACTION_VIEW);
        shortcutIntent.setClassName(context, MainActivity.class.getName());
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
        // 快捷方式的图标
        Intent.ShortcutIconResource iconRes = Intent.ShortcutIconResource.fromContext(context, R.mipmap.ic_launcher);
        shortcut.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconRes);

        context.sendBroadcast(shortcut);

        Toast.makeText(context, "Success", Toast.LENGTH_SHORT).show();
    }

    //
    public static boolean isWifiConnect(Context pContext) {
        boolean result;
        WifiManager mWifiManager = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);
        WifiInfo mConnectionInfo = mWifiManager.getConnectionInfo();
        System.out.println("S -> mConnectionInfo.getBSSID() " + mConnectionInfo.getBSSID());
        System.out.println("S -> mConnectionInfo.getSupplicantState().toString() " + mConnectionInfo.getSupplicantState().toString());
        System.out.println("S -> mConnectionInfo.getSSID() " + mConnectionInfo.getSSID());
        System.out.println("S -> mConnectionInfo.getIpAddress() " + mConnectionInfo.getIpAddress());
        System.out.println("S -> mConnectionInfo.getMacAddress() " + mConnectionInfo.getMacAddress());
        System.out.println("I -> mConnectionInfo.getLinkSpeed() " + mConnectionInfo.getLinkSpeed());
        System.out.println("I -> mConnectionInfo.getNetworkId() " + mConnectionInfo.getNetworkId());
        System.out.println("I -> mConnectionInfo.getRssi() " + mConnectionInfo.getRssi());
        System.out.println("B -> mConnectionInfo.getHiddenSSID() " + mConnectionInfo.getHiddenSSID());

        if (mConnectionInfo.getNetworkId() == -1) {
            result = false;
        } else {
            result = true;
        }

        System.out.println("B -> isWifiConnect " + result);
        return result;
    }

    //
    public static boolean isWifiEnable(Context pContext) {
        boolean result;
        WifiManager mWifiManager = (WifiManager) pContext.getSystemService(Context.WIFI_SERVICE);
        int state = mWifiManager.getWifiState();
        if (state == WifiManager.WIFI_STATE_ENABLED) {
            result = true;
        } else {
            result = false;
        }
        System.out.println("I -> isWifiEnable " + state);
        return result;
    }

    /**
     * @param time
     * @param format like {@code yyyy年MM月dd日 a}
     * @return
     */
    public static String time(long time, String format) {
        String str;
        str = DateFormat.format(format, time).toString();
        return str;
    }
}